package com.icreate.storm.methods.indicators;

import com.icreate.entity.indicator.IndicatorID;
import com.icreate.entity.indicator.S1_Ufield;
import com.icreate.storm.methods.common.DateMethod;
import org.apache.storm.tuple.Values;

/**
 * Created by yc on 2017/5/12.
 */
public class S1_UIndicator extends BaseIndicator {
    public static final String typeName = "mltes1uhttp";

    @Override
    public String getTypeName() {
        return typeName;
    }

    public static final String[] fields = {"type", "cellid", "imsi", "imei", "IndicatorID",
            "mltes1uhttp.tcp_creactlink_success", "mltes1uhttp.tcp_creactlink_request",
            "mltes1uhttp.tcp_creactlink_delay", "mltes1uhttp.tcp_creactlink_count",
            "mltes1uhttp.tcp_creactlink_response_delay", "mltes1uhttp.tcp_creactlink_response_count",
            "mltes1uhttp.tcp_creactlink_confirm_delay", "mltes1uhttp.tcp_creactlink_confirm_count",
            "mltes1uhttp.bp_flow", "mltes1uhttp.bp_delay",
            "mltes1uhttp.sp_delay", "mltes1uhttp.sp_count",
            "mltes1uhttp.service_success", "mltes1uhttp.service_request",
            "mltes1uhttp.qq_service_success", "mltes1uhttp.qq_service_request",
            "mltes1uhttp.wechat_service_success", "mltes1uhttp.wechat_service_request",
            "mltes1uhttp.momo_service_success", "mltes1uhttp.momo_service_request",
            "mltes1uhttp.qidian_reader_service_success", "mltes1uhttp.qidian_reader_service_request",
            "mltes1uhttp.panda_reader_service_success", "mltes1uhttp.panda_reader_service_request",
            "mltes1uhttp.sina_weibo_service_success", "mltes1uhttp.sina_weibo_service_request",
            "mltes1uhttp.tencent_weibo_service_success", "mltes1uhttp.tencent_weibo_service_request",
            "mltes1uhttp.baidu_map_service_success", "mltes1uhttp.baidu_map_service_request",
            "mltes1uhttp.tencent_map_service_success", "mltes1uhttp.tencent_map_service_request",
            "mltes1uhttp.gaode_map_service_success", "mltes1uhttp.gaode_map_service_request",
            "mltes1uhttp.youku_video_service_success", "mltes1uhttp.youku_video_service_request",
            "mltes1uhttp.tencent_video_service_success", "mltes1uhttp.tencent_video_service_request",
            "mltes1uhttp.soku_video_service_success", "mltes1uhttp.soku_video_service_request",
            "mltes1uhttp.aiqiyi_video_service_success", "mltes1uhttp.aiqiyi_video_service_request",
            "mltes1uhttp.qq_music_service_success", "mltes1uhttp.qq_music_service_request",
            "mltes1uhttp.baidu_music_service_success", "mltes1uhttp.baidu_music_service_request",
            "mltes1uhttp.ali_pay_service_success", "mltes1uhttp.ali_pay_service_request",
            "mltes1uhttp.uc_web_service_success", "mltes1uhttp.uc_web_service_request",
            "mltes1uhttp.tonghuashun_service_success", "mltes1uhttp.tonghuashun_service_request",
            "mltes1uhttp.dazhihui_service_success", "mltes1uhttp.dazhihui_service_request",
            "mltes1uhttp.taobao_service_success", "mltes1uhttp.taobao_service_request",
            "mltes1uhttp.jingdong_service_success", "mltes1uhttp.jingdong_service_request",
            "mltes1uhttp.tianmao_service_success", "mltes1uhttp.tianmao_service_request"
    };
    public static final String[] baseFields = {
            "mltes1uhttp.tcp_creactlink_success", "mltes1uhttp.tcp_creactlink_request",
            "mltes1uhttp.tcp_creactlink_delay", "mltes1uhttp.tcp_creactlink_count",
            "mltes1uhttp.tcp_creactlink_response_delay", "mltes1uhttp.tcp_creactlink_response_count",
            "mltes1uhttp.tcp_creactlink_confirm_delay", "mltes1uhttp.tcp_creactlink_confirm_count",
            "mltes1uhttp.bp_flow", "mltes1uhttp.bp_delay",
            "mltes1uhttp.sp_delay", "mltes1uhttp.sp_count",
            "mltes1uhttp.service_success", "mltes1uhttp.service_request",
            "mltes1uhttp.qq_service_success", "mltes1uhttp.qq_service_request",
            "mltes1uhttp.wechat_service_success", "mltes1uhttp.wechat_service_request",
            "mltes1uhttp.momo_service_success", "mltes1uhttp.momo_service_request",
            "mltes1uhttp.qidian_reader_service_success", "mltes1uhttp.qidian_reader_service_request",
            "mltes1uhttp.panda_reader_service_success", "mltes1uhttp.panda_reader_service_request",
            "mltes1uhttp.sina_weibo_service_success", "mltes1uhttp.sina_weibo_service_request",
            "mltes1uhttp.tencent_weibo_service_success", "mltes1uhttp.tencent_weibo_service_request",
            "mltes1uhttp.baidu_map_service_success", "mltes1uhttp.baidu_map_service_request",
            "mltes1uhttp.tencent_map_service_success", "mltes1uhttp.tencent_map_service_request",
            "mltes1uhttp.gaode_map_service_success", "mltes1uhttp.gaode_map_service_request",
            "mltes1uhttp.youku_video_service_success", "mltes1uhttp.youku_video_service_request",
            "mltes1uhttp.tencent_video_service_success", "mltes1uhttp.tencent_video_service_request",
            "mltes1uhttp.soku_video_service_success", "mltes1uhttp.soku_video_service_request",
            "mltes1uhttp.aiqiyi_video_service_success", "mltes1uhttp.aiqiyi_video_service_request",
            "mltes1uhttp.qq_music_service_success", "mltes1uhttp.qq_music_service_request",
            "mltes1uhttp.baidu_music_service_success", "mltes1uhttp.baidu_music_service_request",
            "mltes1uhttp.ali_pay_service_success", "mltes1uhttp.ali_pay_service_request",
            "mltes1uhttp.uc_web_service_success", "mltes1uhttp.uc_web_service_request",
            "mltes1uhttp.tonghuashun_service_success", "mltes1uhttp.tonghuashun_service_request",
            "mltes1uhttp.dazhihui_service_success", "mltes1uhttp.dazhihui_service_request",
            "mltes1uhttp.taobao_service_success", "mltes1uhttp.taobao_service_request",
            "mltes1uhttp.jingdong_service_success", "mltes1uhttp.jingdong_service_request",
            "mltes1uhttp.tianmao_service_success", "mltes1uhttp.tianmao_service_request"
    };

    @Override
    public String[] getBaseFields() {
        return baseFields;
    }

    private static AppType[] appTypes =
            {
                    new AppType(1, 5),//QQ业务
                    new AppType(1, 9),//微信业务
                    new AppType(1, 26),//陌陌业务
                    new AppType(2, 3),//起点阅读业务
                    new AppType(2, 4),//熊猫看书业务
                    new AppType(3, 2),//新浪微博业务
                    new AppType(3, 3),//腾讯微博业务
                    new AppType(4, 3),//百度地图业务
                    new AppType(4, 5),//腾讯地图业务
                    new AppType(4, 9),//高德地图业务
                    new AppType(5, 3),//优酷业务
                    new AppType(5, 6),//腾讯视频业务
                    new AppType(5, 7),//搜狐视频业务
                    new AppType(5, 17),//爱奇艺视频业务
                    new AppType(6, 2),//QQ音乐业务
                    new AppType(6, 5),//百度音乐业务
                    new AppType(9, 3),//支付宝业务
                    new AppType(15, 21),//UC网站访问业务
                    new AppType(16, 3),//同花顺业务
                    new AppType(16, 4),//大智慧业务
                    new AppType(18, 1),//淘宝业务
                    new AppType(18, 2),//京东业务
                    new AppType(18, 18)//天猫业务
            };

    public String[] getFields() {
        return fields;
    }

    public Values getValues(Object s) {
        S1_Ufield field = (S1_Ufield) s;

        Values values = new Values("mltes1uhttp", field.getCellid(), field.getImsi(), field.getImei(), field);
        //TCP建链
        setTCP(field, values);
        setHttp(field, values);
        //业务成功率
        setService(field, values);

        return values;
    }

    private void setTCP(S1_Ufield field, Values values) {

        long count = 0L, sum = 0L;
        //
        count = 0L;
        sum = 0L;
        if (field.getTcp_link_status() != "") {
            long tcp_link_status = this.getValue(field.getTcp_link_status());
            if (tcp_link_status == 0L)
                count = 1;
            if (tcp_link_status == 0L || tcp_link_status == 1L)
                sum = 1;
        }
        values.add(count);//TCP建链成功
        values.add(sum);//TCP建链请求

        //时延
        if (field.getTcp_creactlink_response_delay() != null && field.getTcp_creactlink_confirm_delay() != null) {
            long tcp_creactlink_response_delay = this.getValue(field.getTcp_creactlink_response_delay());
            long tcp_creactlink_confirm_delay = this.getValue(field.getTcp_creactlink_confirm_delay());
            values.add(tcp_creactlink_response_delay + tcp_creactlink_confirm_delay);//TCP建链时延
            values.add(1L);//count
            values.add(tcp_creactlink_response_delay);//TCP建链响应时延
            values.add(1L);//count
            values.add(tcp_creactlink_confirm_delay);//TCP建链确认时延
            values.add(1L);//count
        } else {
            values.add(0L);//TCP建链时延
            values.add(0L);//count
            values.add(0L);//TCP建链响应时延
            values.add(0L);//count
            values.add(0L);//TCP建链确认时延
            values.add(0L);//count
        }
    }

    private void setHttp(S1_Ufield field, Values values) {
        long app_status = this.getValue(field.getApp_status());
        long dl_data = this.getValue(field.getDl_data());
//        String http_lastpacket_1streq_delay_str = cells[77];
        long http_lastpacket_1streq_delay = this.getValue(field.getHttp_lastpacket_1streq_delay());
        long tcp_1strequest_delay = getValue(field.getTcp_1strequest_delay());
        long tcp_creactlink_response_delay = this.getValue(field.getTcp_creactlink_response_delay());
        long tcp_creactlink_confirm_delay = this.getValue(field.getTcp_creactlink_confirm_delay());

        if (!field.getApp_status().equals("") &&
                app_status == 0 &&
                (dl_data / 1024) > 500 &&
                http_lastpacket_1streq_delay != 0) {
            values.add(dl_data * 8 / 1024);
            values.add(http_lastpacket_1streq_delay);
        } else {
            values.add(0L);//http大包下载
            values.add(0L);
        }
        if (!field.getApp_status().equals("") &&
                (dl_data / 1024) < 30 &&
                http_lastpacket_1streq_delay != 0 &&
                tcp_1strequest_delay < 300000) {
            values.add(tcp_creactlink_response_delay + tcp_creactlink_confirm_delay + tcp_1strequest_delay + http_lastpacket_1streq_delay);
            values.add(1L);
        } else {
            values.add(0L);//http小包时延
            values.add(0L);
        }
    }


    private void setService(S1_Ufield field, Values values) {

        boolean valid;


        valid = !(field.getApp_status().equals("") || field.getApp_type().equals("") || field.getApp_sub_type().equals(""));
        long app_type = this.getValue(field.getApp_type());
        long app_sub_type = this.getValue(field.getApp_sub_type());
        long app_status = this.getValue(field.getApp_status());
        if (!field.getApp_status().equals("")) {
            if (app_status == 0)
                values.add(1L);
            else
                values.add(0L);
            values.add(1L);
        } else {
            values.add(0L);
            values.add(0L);
        }

        for (AppType appType : appTypes) {
            if (!valid) {
                values.add(0L);
                values.add(0L);
            } else {
                values.add(getSuccess(app_type, app_sub_type, app_status, appType));
                values.add(getRequest(app_type, app_sub_type, app_status, appType));
            }
        }
    }

    private long getRequest(long app_type, long app_sub_type, long app_status, AppType appType) {
        if (app_type == appType.appType &&
                app_sub_type == appType.appSubType)
            return 1L;
        else
            return 0L;
    }

    private long getSuccess(long app_type, long app_sub_type, long app_status, AppType appType) {
        if (app_type == appType.appType &&
                app_sub_type == appType.appSubType &&
                app_status == 0)
            return 1L;
        else
            return 0L;
    }


    private static class AppType {
        int appType;
        int appSubType;

        public AppType(int appType, int appSubType) {
            this.appType = appType;
            this.appSubType = appSubType;
        }
    }

}
